package com.kukudexin.AdvanceThread.logThread;

import java.util.concurrent.locks.LockSupport;

/**
 * @author: 酷酷的鑫
 * @Date: 14:05
 * @description:
 */
public class MayiktFutureTask<V> implements Runnable {
    private MayiktCallable<V> mayiktCallable;
    private V result;
    private Thread cuThread;

    public MayiktFutureTask(MayiktCallable<V> mayiktCallable) {
        this.mayiktCallable = mayiktCallable;
    }

    @Override
    public void run() {
        result = mayiktCallable.call();
        if (cuThread != null){
            //唤醒阻塞的线程
            LockSupport.unpark(cuThread);
        }
    }
    public V get() {
        // 主线程阻塞
        cuThread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "_____>");
        LockSupport.park();
        // 此时cuThread就是main线程
        System.out.println(Thread.currentThread().getName() + "_____>");
        return result;
    }
}
